﻿using Demo.Application.Dtos.User;
using Demo.Application.Services.User;
using Microsoft.AspNetCore.Mvc;
using PmSoft.Data.Abstractions;
using PmSoft.Web.Abstractions;
using PmSoft.Web.Abstractions.Attributes;

namespace Demo.SqlSugar.WebApi.Controllers
{
	/// <summary>
	/// 
	/// </summary>
	[Route("api/[controller]")]
	[ApiController]
	public class UserController : ControllerBase
	{
		private readonly UserService userService;

		public UserController(UserService userService)
		{
			this.userService = userService;
		}

		[HttpPost("create")]
		[Transaction]
		public async ValueTask<ApiResult> CreateAsync(UserCreateInput form)
		{
			await userService.CreateAsync(form);
			return ApiResult.Ok("创建成功");
		}


		[HttpPost("update")]
		public async ValueTask<ApiResult> UpdateAsync(UserUpdateInput form)
		{
			await userService.UpdateAsync(form);
			return ApiResult.Ok("更新成功");
		}

		[HttpGet("user")]
		public async ValueTask<UserDto> GetUserAsync(int userId = 1)
		{
			var user = await userService.GetUserAsync(userId);
			return user;
		}

		[HttpGet("users")]
		public async ValueTask<ApiResult<IEnumerable<UserDto>>> GetUsersAsync(int deptId = 1)
		{
			var users = await userService.GetUsersAsync(deptId);
			return ApiResult<IEnumerable<UserDto>>.Ok(users);
		}

		[HttpPost("page")]
		public async ValueTask<IPagedList<UserDto>> PageAsync(UserQuery query, int pageIndex = 1, int pageSize = 20)
		{
			var users = await userService.UserPagingAsync(query, pageIndex, pageSize);
			return users;
		}
	}
}
